package com.zzf.sus_time
import android.content.*
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.widget.Toast

object OverlayPermissionHelper {

    fun requestOverlayPermission(context: Context) {
        val intent = when {
            isMiui() -> miuiPermissionIntent(context)
            isHuawei() -> huaweiPermissionIntent(context)
            isOppo() -> oppoPermissionIntent(context)
            isVivo() -> vivoPermissionIntent(context)
            else -> defaultOverlayIntent(context)
        }

        try {
            context.startActivity(intent)
            Toast.makeText(context, "请授予悬浮窗权限", Toast.LENGTH_SHORT).show()
        } catch (e: Exception) {
            // fallback 到默认页面
            try {
                context.startActivity(defaultOverlayIntent(context))
                Toast.makeText(context, "请手动开启悬浮窗权限", Toast.LENGTH_SHORT).show()
            } catch (e: Exception) {
                Toast.makeText(context, "无法打开权限设置页面，请手动设置", Toast.LENGTH_LONG).show()
            }
        }
    }

    private fun defaultOverlayIntent(context: Context): Intent {
        return Intent(
            Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:${context.packageName}")
        ).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
    }

    private fun isMiui(): Boolean = Build.MANUFACTURER.equals("Xiaomi", ignoreCase = true)
    private fun isHuawei(): Boolean = Build.MANUFACTURER.equals("Huawei", ignoreCase = true)
    private fun isOppo(): Boolean = Build.MANUFACTURER.equals("Oppo", ignoreCase = true)
    private fun isVivo(): Boolean = Build.MANUFACTURER.equals("Vivo", ignoreCase = true)

    private fun miuiPermissionIntent(context: Context): Intent {
        return Intent("miui.intent.action.APP_PERM_EDITOR").apply {
            setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity")
            putExtra("extra_pkgname", context.packageName)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
    }

    private fun huaweiPermissionIntent(context: Context): Intent {
        return Intent().apply {
            setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            component = ComponentName(
                "com.huawei.systemmanager",
                "com.huawei.permissionmanager.ui.MainActivity"
            )
        }
    }

    private fun oppoPermissionIntent(context: Context): Intent {
        return Intent().apply {
            setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            putExtra("packageName", context.packageName)
            component = ComponentName(
                "com.coloros.safecenter",
                "com.coloros.safecenter.permission.floatwindow.FloatWindowListActivity"
            )
        }
    }

    private fun vivoPermissionIntent(context: Context): Intent {
        return Intent().apply {
            setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            component = ComponentName(
                "com.iqoo.secure",
                "com.iqoo.secure.ui.phoneoptimize.FloatWindowManager"
            )
        }
    }
}
